package com.kwlt.smc.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import com.kwlt.smc.entity.CallBox;
import com.kwlt.smc.entity.ErrorStatus;
import com.kwlt.smc.entity.Returnsms;
import com.kwlt.smc.entity.StatusBox;

public class XmlHttpUtil {
		
	public static Returnsms getReturnsms(String xml){
		Returnsms rt = new Returnsms();
		byte[] b;
		try {
			b = xml.getBytes("gb2312");
			InputStream is = new ByteArrayInputStream(b);
			SAXBuilder builder = new SAXBuilder();
			Document document = builder.build(is);
			Element element = document.getRootElement();
			rt.setReturnstatus(element.getChildText("returnstatus"));
			rt.setMessage(element.getChildText("message"));
			rt.setRemainpoint(element.getChildText("remainpoint"));
			rt.setSuccessCounts(element.getChildText("successCounts"));
			rt.setTaskID(element.getChildText("taskID"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		 catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rt;
	}
	
	
	public static ErrorStatus getErrorStatus(String xml){
		ErrorStatus error = new ErrorStatus();
		byte[] b = xml.getBytes();
		InputStream is = new ByteArrayInputStream(b);
		SAXBuilder builder = new SAXBuilder();
		try {
			Document document = builder.build(is);
			Element element = document.getRootElement();
			Element child = element.getChild("errorstatus");
			String childText = child.getChildText("error");
			String remark = child.getChildText("remark");
			error.setError(childText);
			error.setRemark(remark);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return error;
	}
	
	public static List<StatusBox> getStatusBox(String xml){
		List<StatusBox> list = new ArrayList<StatusBox>();
		byte[] b = xml.getBytes();
		InputStream is = new ByteArrayInputStream(b);
		SAXBuilder builder = new SAXBuilder();
		try {
			Document document = builder.build(is);
			Element element = document.getRootElement();
			List<Element> eList = element.getChildren("statusbox");
			for(Element e:eList){
				StatusBox box = new StatusBox();
				box.setMobile(e.getChildText("mobile"));
				box.setReceivetime(e.getChildText("receivetime"));
				box.setStatus(e.getChildText("status"));
				box.setTaskid(e.getChildText("taskid"));
				list.add(box);
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public static List<CallBox> getCallBox(String xml){
		List<CallBox> list = new ArrayList<CallBox>();
		byte[] b = xml.getBytes();
		InputStream is = new ByteArrayInputStream(b);
		SAXBuilder builder = new SAXBuilder();
		try {
			Document document = builder.build(is);
			Element element = document.getRootElement();
			List<Element> eList = element.getChildren("callbox");
			for(Element e:eList){
				CallBox box = new CallBox();
				box.setMobile(e.getChildText("mobile"));
				box.setReceivetime(e.getChildText("receivetime"));
				box.setContent(e.getChildText("content"));
				box.setTaskid(e.getChildText("taskid"));
				list.add(box);
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public static boolean isError(String xml){
		byte[] b = xml.getBytes();
		InputStream is = new ByteArrayInputStream(b);
		SAXBuilder builder = new SAXBuilder();
		try {
			Document document = builder.build(is);
			Element element = document.getRootElement();
			Element child = element.getChild("errorstatus");
			if(child==null){
				return false;
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	public static void main(String[] args) {
		String xml = "<?xml version='1.0' encoding='utf-8'?><returnsms><statusbox><mobile>15023239810</mobile><taskid>1212</taskid>" +
				"<status>10</status><receivetime>2011-12-02 22:12:11</receivetime></statusbox><statusbox><mobile>15023239811</mobile>" +
				"<taskid>1212</taskid><status>20</status><receivetime>2011-12-02 22:12:11</receivetime></statusbox></returnsms>";
		
		byte[] b = xml.getBytes();
		InputStream is = new ByteArrayInputStream(b);
		SAXBuilder builder = new SAXBuilder();
		try {
			Document document = builder.build(is);
			Element element = document.getRootElement();
			List<Element> list = element.getChildren("statusbox");
			for(Element e:list){
				System.out.println(e.getChildText("mobile"));
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

